
cc.Class({
    extends: cc.Component,

    properties: {
        houses: {
            default: [],
            type: [cc.Node],
            displayName: '房子组件'
        },

        virtuals: {
            default: [],
            type: [cc.Node],
            displayName: '实际房子'
        },
    },

    onLoad() {
        this.canvasBox = cc.find("Canvas");
        this.node.on('realEgo', this.realEgo, this);

        for (let i = 0; i < this.houses.length; i++) {
            this.houses[i].on(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
            this.houses[i].on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
            this.houses[i].on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
            this.houses[i].on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
        }

        this.houseSizes = [{width:50,height:50},{width:100,height:20},{width:20,height:30},{width:20,height:30},{width:10,height:40}];
        this.virtualSizes = [{width:200,height:200},{width:300,height:50},{width:40,height:60},{width:60,height:100},{width:20,height:100}];
    },

    start() {
        // 缓存原始父节点
        this._oldPosition = null;
        this._oldParent = cc.find('Canvas');

        // 自我点
        this.RealPosition = null;
        this.RealParent = null;
    },

    realEgo(ev) {
        let data = ev.detail;
        switch (data.action) {
            case 'OCCUPY':
                console.log(data.tag);

                this.RealPosition = this.virtuals[data.tag - 1].position;
                this.RealParent = this._oldParent;
                break;
            case 'LEAVE':
                this.RealPosition = null;
                this.RealParent = null;
                break;
            default:
                break;
        }
    },

    _onTouchStart(touchEvent) {
        let curNode = touchEvent.target;
        this._oldPosition = cc.Vec2(curNode.x,curNode.y);

        let location = touchEvent.getLocation();

        curNode.position = this.canvasBox.convertToNodeSpaceAR(location);
        curNode.parent = this.canvasBox;
    },

    _onTouchMove(touchEvent) {
        let curNode = touchEvent.target;
        let location = touchEvent.getLocation();
        curNode.position = this.canvasBox.convertToNodeSpaceAR(location);
    },

    _onTouchEnd(touchEvent) {
        this._handleTouchExit(touchEvent);
    },

    _onTouchCancel(touchEvent) {
        this._handleTouchExit(touchEvent);
    },

    _handleTouchExit(touchEvent) {
        let curNode = touchEvent.target;
        if (this.RealPosition) {
            curNode.parent = this._oldParent;
            curNode.position = this.RealPosition;

            this.RealPosition = null;
        } else {
            curNode.parent = this._oldParent;
            curNode.position = this._oldPosition;
        }
    },

    removeEvent() {
        // this.houseBody.off(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
        // this.houseBody.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        // this.houseBody.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        // this.houseBody.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
    },

    onEnable: function () {
        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;
        // cc.director.getCollisionManager().enabledDrawBoundingBox = true;
    },

    onDisable: function () {
        cc.director.getCollisionManager().enabled = false;
        // cc.director.getCollisionManager().enabledDebugDraw = false;
        // cc.director.getCollisionManager().enabledDrawBoundingBox = false;
    },

    update(dt) {
        for (let i = 0; i < this.houses.length; i++) {
            if (this.houses[i].x > -250 && this.houses[i].width !== 200) {
                this.houses[i].width = this.virtualSizes[i].width;
                this.houses[i].height = this.virtualSizes[i].height;
    
                this.virtuals[i].opacity = 255;
            } else if(this.houses[i].x < -250 && this.houses[i].width !== 50) {
                this.houses[i].width = this.houseSizes[i].width;
                this.houses[i].height = this.houseSizes[i].height;
                this.virtuals[i].opacity = 0;
            }
        }
    },
});
